﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.Hosting;

namespace CPPEI.Coolzon.ManagementCenter.Common
{
    public class MenuConfig
    {
        private string _XMLpath;
        private static MenuConfig dal = null;
        private MenuConfig()
        {
            InitXMLpath();
        }

        public static MenuConfig Current
        {
            get
            {
                if (dal == null)
                    dal = new MenuConfig();

                return dal;
            }
        }

        private void InitXMLpath()
        {
            string xmlpath = "~/Config/Menus.xml";
            _XMLpath = HostingEnvironment.MapPath(xmlpath);
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, _XMLpath);
            permission.Demand();
        }

        private DataTable GetAllMenus()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(_XMLpath);
            return ds.Tables[0];
        }

        private DataTable GetAllMenus(int parentid)
        {
            DataTable dt = GetAllMenus();
            DataTable _dt = dt.Clone();
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["parentid"].ToString() == parentid.ToString())
                    _dt.Rows.Add(dr.ItemArray);
            }

            return _dt;
        }

        public string CreateHTML()
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = GetAllMenus(0);
            DataTable dt2 = null;
            string _tempHtml;
            foreach (DataRow dr in dt.Rows)
            {

                sb.Append("{title:'" + dr["title"].ToString() + "',autoScroll:true,border:false,iconCls:'nav',");

                dt2 = GetAllMenus(int.Parse(dr["id"].ToString()));
                if (dt2.Rows.Count > 0)
                {
                    _tempHtml = "<ul class=\"LeftNav\">";
                    foreach (DataRow dr2 in dt2.Rows)
                    {
                        _tempHtml += "<li><a target=\"main\" href=\"" + dr2["url"].ToString() + "\"> " + dr2["title"].ToString() + "</a></li>";
                    }

                    if (_tempHtml != "<ul>")
                    {
                        _tempHtml += "</ul>";
                        sb.Append("html:'" + _tempHtml + "'}");
                    }
                    else
                    {
                        sb.Append("html:''}");
                    }
                }
                else
                {
                    sb.Append("html:''}");
                }

                sb.Append(",");
            }

            return sb.ToString().TrimEnd(',');
        }

        public string CreateEASYHTML()
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = GetAllMenus(0);
            DataTable dt2 = null;
            string _tempHtml;
            foreach (DataRow dr in dt.Rows)
            {

                sb.Append("<div title='" + dr["title"].ToString() + "' style='padding:10px; text-align:left; float:left;'>");

                dt2 = GetAllMenus(int.Parse(dr["id"].ToString()));
                if (dt2.Rows.Count > 0)
                {
                    _tempHtml = "<ul class=\"LeftNav\">";
                    foreach (DataRow dr2 in dt2.Rows)
                    {
                        _tempHtml += "<a target=\"main\"  href=\"" + dr2["url"].ToString() + "\"> " + dr2["title"].ToString() + "</a>";
                    }

                    if (_tempHtml != "<ul>")
                    {
                        _tempHtml += "</ul>";
                        sb.Append(_tempHtml + " </div>");
                    }
                    else
                    {
                        sb.Append(" </div>");
                    }
                }
                else
                {
                    sb.Append(" </div>");
                }

                //sb.Append(",");
            }

            return sb.ToString().TrimEnd(',');
        }
    }
}
